﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SQLite;
using System.Drawing;

namespace festivalDatabase
{
    /// <summary>
    /// This class is never instantiated. It contains many useful methods that are
    /// needed by other classes, so it serves as a base class to those classes.
    /// It implements the Form class so that the classes inheriting from it
    /// can be GUIs.
    /// </summary>
    public class userInterface : Form
    {
        /// <summary>
        /// Various variables for storing state.
        /// </summary>
        protected dbAccessor dbAccess;
        protected DataTable currentUser;
        protected DataTable festivals;
        protected int currentFestivalID;
        public enum festivalTypes { Music, Art };
        protected festivalTypes currentFestivalType;

        /// <summary>
        /// Sets the icon for the current Form, enables the borderstyle, and disables
        /// resizing and/or maximizing.
        /// </summary>
        protected void setIcon()
        {
            Icon = new Icon("icon.ico");
            this.FormBorderStyle = FormBorderStyle.Fixed3D;
            this.MaximizeBox = false;
        }

        /// <summary>
        /// Formats a given date string to a format that is "friendly" to masked text boxes.
        /// </summary>
        /// <param name="date">the date to be formatted</param>
        /// <returns>the resulting string</returns>
        protected string formatDateStringForDisplay(string date)
        {
            char[] d = date.ToCharArray();
            char[] result = new char[10];
            int resultPosition = 0;

            try
            {
                char c = d[0];
                int monthLength = 0;

                while (c != '/')
                {
                    monthLength++;
                    c = d[monthLength];
                }

                if (monthLength != 2)
                    result[resultPosition++] = '0';

                for (int i = 0; i < monthLength; i++)
                {
                    result[resultPosition++] = d[i];
                }

                int dayLength = monthLength + 1;
                c = d[dayLength];

                while (c != '/')
                {
                    dayLength++;
                    c = d[dayLength];
                }

                if (dayLength != monthLength + 3)
                    result[resultPosition++] = '0';

                for (int i = monthLength + 1; i < dayLength; i++)
                {
                    result[resultPosition++] = d[i];
                }

                int yearLength = dayLength + 1;
                c = d[yearLength];

                while (c != ' ')
                {
                    yearLength++;
                    c = d[yearLength];
                }

                if (yearLength != dayLength + 5)
                {
                    for (int i = dayLength + 1; i < yearLength; i++)
                        result[resultPosition++] = '0';
                }

                for (int i = dayLength + 1; i < yearLength; i++)
                {
                    result[resultPosition++] = d[i];
                }

                char[] final = new char[resultPosition];

                for (int i = 0; i < resultPosition; i++)
                    final[i] = result[i];

                return new string(final);
            }
            catch
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Returns the last 4 characters of a string (VERY commonly the festival number).
        /// </summary>
        /// <param name="festivalNameCommaNumber">just a string (commonly of the form "festivalName, ####)"</param>
        /// <returns>the last 4 characters of the argument</returns>
        protected string getFestivalNumber(string festivalNameCommaNumber)
        {
            char[] nameAndNumber = festivalNameCommaNumber.ToCharArray();
            int index = nameAndNumber.Length-4;
            char c = nameAndNumber[index];
            
            char[] result = new char[4];
            int resultIndex = 0;

            for (; index < nameAndNumber.Length; index++ )
                result[resultIndex++] = nameAndNumber[index];

            return new string(result);
        }

        /// <summary>
        /// Parses a string and counts the number of characters that are valid ints.
        /// </summary>
        /// <param name="input">string to be parsed</param>
        /// <param name="desiredLength">the preferred length of the number you're checking (phone_num = 10)</param>
        /// <param name="lengthZeroOK">true if it's OK for the length to be zero and false if it's not OK</param>
        /// <returns>boolean indicating whether the string contained a valid number of ints</returns>
        protected bool validNumLength(string input, int desiredLength, bool lengthZeroOK)
        {
            char[] num = input.ToCharArray();
            char[] realNum = new char[10];
            int index = 0;

            for (int i = 0; i < num.Length; i++)
            {
                if (num[i] == '0' || num[i] == '1' || num[i] == '2' || num[i] == '3' || num[i] == '4' || num[i] == '5' || num[i] == '6' || num[i] == '7' || num[i] == '8' || num[i] == '9')
                    realNum[index++] = num[i];
            }

            if ((index == 0 && lengthZeroOK) || index == desiredLength)
                return true;

            return false;
        }

        /// <summary>
        /// Takes a date in the form mm/dd/yyyy and returns a date in a valid database
        /// DateTime format.
        /// </summary>
        /// <param name="genericDate">date mm/dd/yyyy</param>
        /// <returns>database DateTime</returns>
        protected string formatGenericDate(string genericDate)
        {
            char[] date = genericDate.ToCharArray();

            char[] reformatted = new char[date.Length];

            reformatted[0] = date[date.Length - 4];
            reformatted[1] = date[date.Length - 3];
            reformatted[2] = date[date.Length - 2];
            reformatted[3] = date[date.Length - 1];
            reformatted[4] = '-';
            reformatted[5] = date[0];
            reformatted[6] = date[1];
            reformatted[7] = '-';
            reformatted[8] = date[3];
            reformatted[9] = date[4];

            string result = new string(reformatted);

            return result;
        }

        /// <summary>
        /// NOT USED - this class is never instantiated directly!
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // userInterface
            // 
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Name = "userInterface";
            this.ResumeLayout(false);
        }
    }
}
